/**
 * @Author yinyunqi
 * @date 2018年1月11日
 * @Content 
 */
package com.yongqi.controller.console;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.abel533.echarts.Option;
import com.yongqi.model.Family;
import com.yongqi.model.console.Debt;
import com.yongqi.model.console.Remind;
import com.yongqi.pub.PrincipalFormat;
import com.yongqi.service.console.DebtService;
import com.yongqi.service.console.MainService;
import com.yongqi.service.console.RemindService;

@Controller
@RequestMapping("/main")
public class MainController {
	@Autowired
	MainService mainService;
	@Autowired
	DebtService debtService;
	@Autowired
	RemindService remindService;

	/**
	 * 处理跳转到主页面
	 * 
	 * @return
	 */
	@RequestMapping("/index")
	public String MainIndex(Model model) {
		String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		Map<String, String> map = new HashMap<String, String>();
		map.put("familyAccount", familyAccount);
		// 获取当月收入
		String incomeTotal = mainService.currentBill("income");
		// 获取当月支出
		String payTotal = mainService.currentBill("pay");
		// 获取当月预算
		String debuget = mainService.avgBudget();
		// 页面提示信息
		// 七天到期还款
		List<Debt> endDebtList = debtService.endDebt(map);
		// 今天到期还款
		List<Debt> todayEndDebtList = debtService.todayEndDebt(map);
		// 已经到期还款
		List<Debt> endedDebtList = debtService.endedDebt(map);
		// 待办事项
		List<Remind> remindList = remindService.queryComingRemind(map);
		model.addAttribute("incomeTotal", incomeTotal);
		model.addAttribute("payTotal", payTotal);
		model.addAttribute("debuget", debuget);
		model.addAttribute("endDebtList", endDebtList);
		model.addAttribute("todayEndDebtList", todayEndDebtList);
		model.addAttribute("endedDebtList", endedDebtList);
		model.addAttribute("remindList", remindList);
		return "MainIndex";
	}

	/**
	 * 主页面折线图
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/lineChart")
	public String lineChart() {

		Option option = mainService.lineChart();
		JSONObject object = new JSONObject();
		object.put("option", option);
		return object.toJSONString().replaceAll("@", "\",\"");
	}

	@ResponseBody
	@RequestMapping("/partReload")
	public String partReload(Model model) {
		System.out.println("partReload");
		Map<String, String> map = PrincipalFormat.familyAccount();
		// 页面提示信息
		// 七天到期还款
		List<Debt> endDebtList = debtService.endDebt(map);
		// 今天到期还款
		List<Debt> todayEndDebtList = debtService.todayEndDebt(map);
		// 已经到期还款
		List<Debt> endedDebtList = debtService.endedDebt(map);
		// 待办事项
		List<Remind> remindList = remindService.queryComingRemind(map);
		String html = "<li class=\"ll\">";
		// 七天到期还款
		if (endDebtList.size() > 0) {
			html += "<ul>";
			for (Debt debt : endDebtList) {
				html += "<a href=\"/gradesign/debt/index\"><li><p style=\"color:green\">七日内到期借款：" + debt.getItem()
						+ "</p></li></a>\r\n";
			}
			html += "</ul> ";
		}
		// 今天到期还款
		if (todayEndDebtList.size() > 0) {
			html += "<ul>";
			for (Debt debt : todayEndDebtList) {
				html += "<a href=\"/gradesign/debt/index\"><li><p style=\"color:yellow\">今日到期借款：" + debt.getItem()
						+ "</p></li></a>\r\n";
			}
			html += "</ul> ";
		}
		// 已经到期还款
		if (endedDebtList.size() > 0) {
			html += "<ul>";
			for (Debt debt : endedDebtList) {
				html += "<a href=\"/gradesign/debt/index\"><li><p style=\"color:red\">已到期借款：" + debt.getItem()
						+ "</p></li></a>\r\n";
			}
			html += "</ul> ";
		}
		// 备忘提醒
		if (remindList.size() > 0) {
			html += "<ul>";
			for (Remind remind : remindList) {
				html += "<a href=\"/gradesign/debt/index\"><li><p style=\"color:blue\">" + remind.getPlandate() + "提醒："
						+ remind.getMsg() + "</p></li></a>\r\n";
			}
			html += "</ul> ";
		}
		html += "</li>";
		model.addAttribute("endDebtList", endDebtList);
		model.addAttribute("todayEndDebtList", todayEndDebtList);
		model.addAttribute("endedDebtList", endedDebtList);
		model.addAttribute("remindList", remindList);
		JSONObject object = new JSONObject();
		object.put("result", html);
		return object.toJSONString();
	}
}
